/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.ui;

import java.awt.font.GlyphVector;


/**
 * The text selection shape.
 * 
 * @see Position
 * @see Caret
 * @author jdp
 */
public final class Selection
    extends GV
    implements GV.Aware
{
    public final float first, left, right, last;

    public final int start, end;


    public Selection(GV g, Position p){
        this(g,p.getOffsetLeading(),p.getEnd());
    }
    public Selection(GV g, int start, int end){
        super(g);
        GlyphVector[] glyphs = this.glyphs;
        float lineheight = this.lineheight;
        float cix = this.ix, ciy = this.iy;

        if (0 > start)
            start = 0;

        this.start = start;
        this.end = end;
        if (start < end){

            float padl = cix;
            float bx = cix;
            float by = ciy;
            float br;

            float first = 0f, left = 0f, right = 0f, last = 0f;
            float[] glp = new float[2];
            int gvstart = 0, gvend;

            for (int cc = 0, ccc = (null != glyphs)?(glyphs.length):(0); cc < ccc; cc++){
                GlyphVector gv = glyphs[cc];
                int gvc = gv.getNumGlyphs();
                gvend = gvstart + gvc;
                if (gvstart <= start && start <= gvend){
                    int rstart = (start-gvstart);
                    int rcount = Math.min( (end-start), (gvc-rstart));
                    if (0 < rcount){
                        glp = GV.PositionsLR(gv,rstart,rcount,glp);

                        bx = (padl + glp[0]);

                        br = (padl + glp[1]);

                        if (0f == first){
                            first = bx;
                            left = bx;
                            right = br;
                        }
                        else {
                            left = Math.min(left,bx);
                            right = Math.max(right,br);
                        }
                        last = br;
                        {
                            this.moveTo(bx,by);
                            this.lineTo(br,by);
                            by += lineheight;
                            this.lineTo(br,by);
                            this.lineTo(bx,by);
                            by -= lineheight;
                            this.lineTo(bx,by);
                        }
                        start += rcount;
                        if (start >= end)
                            break;
                    }
                }
                gvstart += gvc;
                by += lineheight;
            }
            this.first = first;
            this.left = left;
            this.right = right;
            this.last = last;
        }
        else {
            this.first = 0f;
            this.left = 0f;
            this.right = 0f;
            this.last = 0f;
        }
    }

    public boolean is(Position p){
        if (null == p)
            return false;
        else
            return (this.start == p.offset)&&(this.end == p.getEnd());
    }
    public boolean isNot(Position p){
        if (null == p)
            return true;
        else
            return (this.start != p.offset)||(this.end != p.getEnd());
    }
}
